/*
 * @Author: Guo Lulu 
 * @Date: 2019-02-25 15:19:26 
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2019-04-09 09:18:17
 * @Description: iframe自适应，首页加载菜单，监听菜单点击事件，伸缩左边菜单，手机设备的简单适配
 */


layui.config({
	base: './plugins/layui/modules/'
}).use(['layer', 'navbar','http', 'tab'], function() {
	var $ = layui.jquery,
		layer = layui.layer,
		navbar = layui.navbar();

	var tab = layui.tab({
		elem: '.admin-nav-card', //设置选项卡容器                
		contextMenu: true,
		maxSetting: 10
	});
		
	//iframe自适应
	$(window).on('resize', function() {
		var $content = $('.admin-nav-card .layui-tab-content');
		$content.height($(this).height() - 130); //147默认 88
		$content.find('iframe').each(function() {
			$(this).height($content.height()-4);
		});
	}).resize();


	//加载左边菜单列表
	var userInfo = localStorage.getItem('xi-head-userInfo')
	if(userInfo){
		userInfo = JSON.parse(userInfo)
		//渲染navbar
		navbar.set({
			spreadOne: true,
			elem: '#admin-navbar-side',
			cached: true,
			data: userInfo.authorities
		});
		navbar.render();
	}else{
		location.href = "login.html";
	}
	

	//监听点击事件
	navbar.on('click(side)', function(data) {
		// tab.tabAdd(data.field);
		parameter.ifParSelect = true; // 点击菜单更新面包屑导航
		// 清除记录的搜索条件
		sessionStorage.setItem('xi-head-canteenSearch', '')

		$('#iframe').attr('src', data.field.href);
	});


	//伸缩左边菜单
	$('.admin-side-toggle').on('click', function() {
		var sideWidth = $('#admin-side').width();
		if(sideWidth === 200) {
			$('#admin-body').animate({
				left: '60'
			}); //admin-footer
			$('#admin-footer').animate({
				left: '60'
			});
			$('.layui-layout-left').animate({
				left: '60'
			});
			$('#admin-side').animate({
				width: '60'
			});
			$('.layui-logo').animate({
				width: '60'
			});
			$(this).find('i').removeClass('layui-icon-shrink-right');
			$(this).find('i').addClass('layui-icon-spread-left');
			$('.layui-layout.layui-layout-admin').addClass('layadmin-side-shrink');
			$('.layui-logo').hide()
			
		} else {
			$('#admin-body').animate({
				left: '200px'
			});
			$('#admin-footer').animate({
				left: '200px'
			});
			$('.layui-layout-left').animate({
				left: '200px'
			});
			$('#admin-side').animate({
				width: '200px'
			});
			$('.layui-logo').animate({
				width: '200px'
			});
			$(this).find('i').removeClass('layui-icon-spread-left');
			$(this).find('i').addClass('layui-icon-shrink-right');
			$('.layui-layout.layui-layout-admin').removeClass('layadmin-side-shrink');
			setTimeout(function () {
				$('.layui-logo').show()
			},200)
		}
	});
	//缩进菜单栏hover提示
	var tip_index;
	$(document).on('mouseenter', '.layadmin-side-shrink #admin-navbar-side .layui-nav-item', function() {
		var ctext=$(this).find('a>cite').html();
		tip_index=layer.tips(ctext, this);
	});
	$(document).on('mouseleave', '.layadmin-side-shrink #admin-navbar-side .layui-nav-item', function() {
		layer.close(tip_index);
	});


	//手机设备的简单适配
	var treeMobile = $('.site-tree-mobile'),
		shadeMobile = $('.site-mobile-shade');
	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
		// $('.layui-layout-admin .layui-logo').show();
	});
	$(document).on('click', '.layadmin-side-shrink #admin-navbar-side .layui-nav-item', function() {
		layer.close(tip_index);
		$('#admin-body').animate({
			left: '200px'
		});
		$('#admin-footer').animate({
			left: '200px'
		});
		$('.layui-layout-left').animate({
			left: '200px'
		});
		$('#admin-side').animate({
			width: '200px'
		});
		$('.layui-logo').animate({
			width: '200px'
		});
		$('.admin-side-toggle').find('i').removeClass('layui-icon-spread-left');
		$('.admin-side-toggle').find('i').addClass('layui-icon-shrink-right');
		$('.layui-layout.layui-layout-admin').removeClass('layadmin-side-shrink');
		setTimeout(function () {
			$('.layui-logo').show()
		},200)
	});
	shadeMobile.on('click', function() {
		$('body').removeClass('site-mobile');
		// $('.layui-layout-admin .layui-logo').hide();
	});
});
